<template>
  <div class="commentSectionCont">
    <el-input
      v-model="commentInfo"
      type="textarea"
      placeholder="请输入评论"
      :maxlength="140"
      :show-word-limit="true"
      :rows="3"
      :resize="'none'"
    />
    <div class="publishCommentRow">
      <div>表情/话题/@</div>
      <div>
        <div class="pulbishBtn">评论</div>
      </div>
    </div>
    <div class="newestComments" v-if="playlistCommentData?.hotComments">
      <div class="title">精彩评论</div>
      <div class="list">
        <CommentFloorVue
          v-for="comment in playlistCommentData.hotComments"
          :comment-data="comment"
        />
      </div>
      <div class="moreHotCommentBtnCont">
        <div class="moreHotComments" @click="handleToAllHotComments">
          更多精彩评论>
        </div>
      </div>
    </div>
    <div class="newestComments">
      <div class="title">最新评论({{ playlistCommentData?.total }})</div>
      <div class="list">
        <CommentFloorVue
          v-for="comment in playlistCommentData?.comments"
          :comment-data="comment"
        />
      </div>
      <div class="pagiCont">
        <el-pagination
          small
          layout="prev, pager, next"
          :total="playlistCommentData?.total"
          background
          :pageSize="fixedLimit"
          v-model:currentPage="newestCommentCurrent"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
import { usePlayListComment } from "@/hooks/commentHook";
import CommentFloorVue from "../CommentFloor/CommentFloor.vue";
const fixedLimit = 50;

const props = defineProps<{
  playlistId?: number | string;
  albumnId?: number | string;
  commentSource: keyof {
    albumn: string;
    playlist: string;
    mv: string;
    song: string;
  };
}>();

const router = useRouter();
const newestCommentCurrent = ref(1);
const commentInfo = ref("");
const { playlistCommentData, queryPlaylistComment } = usePlayListComment();
const handleToAllHotComments = () => {
  let type;
  let id;
  switch (props.commentSource) {
    case "albumn":
      type = 3;
      id = props.albumnId;
      break;
    case "playlist":
      type = 2;
      id = props.playlistId;
      break;
    default:
      break;
  }
  if (id && type !== undefined) {
    router.push({
      path: "hotComments",
      query: {
        id,
        type,
      },
    });
  }
};

onMounted(() => {
  if (props.commentSource === "playlist" && props.playlistId) {
    queryPlaylistComment({
      id: props.playlistId,
      offset: (newestCommentCurrent.value - 1) * fixedLimit,
      limit: fixedLimit,
    });
  }
});

watch(
  () => newestCommentCurrent.value,
  (newPage, oldPage) => {
    if (props.commentSource === "playlist" && props.playlistId) {
      queryPlaylistComment(
        {
          id: props.playlistId,
          offset: (newPage - 1) * fixedLimit,
          limit: fixedLimit,
        },
        () => {
          newestCommentCurrent.value = newPage;
        }
      );
    }
  }
);
</script>

<style lang="less">
.commentSectionCont {
  .publishCommentRow {
    color: #666;
    margin-top: 10px;
    height: 32px;
    display: flex;
    justify-content: space-between;
    .pulbishBtn {
      border: 1px solid #dbdbdb;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;

      width: 60px;
      cursor: pointer;
      &:hover {
        background-color: #f2f2f2;
      }
      border-radius: 16px;
    }
  }
  .newestComments {
    margin-top: 10px;
    .moreHotCommentBtnCont {
      display: flex;
      justify-content: center;
      margin: 20px 0;
      .moreHotComments {
        width: 128px;
        cursor: pointer;
        font-size: 14px;
        color: #444;
        &:hover {
          color: #666;
        }
        height: 30px;
        border-radius: 15px;
        border: 1px solid #ddd;
        text-align: center;
        line-height: 30px;
      }
    }
    .title {
      color: #333;
      font-weight: bold;
      font-size: 18px;
    }
    .pagiCont {
      height: 100px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .el-pagination.is-background .btn-next.is-active,
  .el-pagination.is-background .btn-prev.is-active,
  .el-pagination.is-background .el-pager li.is-active {
    background-color: var(--primary-color);
  }
}
</style>
